//
//  NiubiStyleCell_subject.m
//  danpin
//
//  Created by chuangjia on 16/11/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "NiubiStyleCell_subject.h"
#import "UMengShare.h"
@interface NiubiStyleCell_subject()
{
    
}
@property(nonatomic,strong)UIImageView *fuliView;
@property(nonatomic,strong)UIImageView *subjectV1View;
@property(nonatomic,strong)UIImageView *subjectV2View;
@property(nonatomic,strong) UIView * backView;

@end
@implementation NiubiStyleCell_subject

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
       
        _backView = [[UIView alloc]initWithFrame:CGRectZero];
        _backView.backgroundColor = [UIColor whiteColor];
        _backView.layer.cornerRadius = 3;//(值越大，角就越圆)
        _backView.layer.masksToBounds = YES;
        [self.contentView addSubview:_backView];
        
        self.backgroundColor = [UIColor colorWithRed:0.967 green:0.967 blue:0.967 alpha:1];
        
        _fuliView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _fuliView.backgroundColor= [UIColor clearColor];
        [self.contentView addSubview:_fuliView];
        
        _fuliView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickFuli:)];
        [_fuliView addGestureRecognizer:tapGestureRecognizer];
        _fuliView.tag = 0;
        tapGestureRecognizer=nil;
        
        
        //
        _subjectV1View =[[UIImageView alloc] initWithFrame:CGRectZero];
        _subjectV1View.backgroundColor= [UIColor clearColor];//UIColorFromRGB(0xe5e5e5);
        [self.contentView addSubview:_subjectV1View];
        
        _subjectV1View.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGestureRecognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickFuli:)];
        [_subjectV1View addGestureRecognizer:tapGestureRecognizer1];
        tapGestureRecognizer1=nil;
        _subjectV1View.tag = 1;
        
        
        //
        _subjectV2View =[[UIImageView alloc] initWithFrame:CGRectZero];
        _subjectV2View.backgroundColor= [UIColor clearColor];//UIColorFromRGB(0xe5e5e5);
        [self.contentView addSubview:_subjectV2View];
        
        
        _subjectV2View.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGestureRecognizer2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickFuli:)];
        [_subjectV2View addGestureRecognizer:tapGestureRecognizer2];
        
        tapGestureRecognizer2=nil;
        _subjectV2View.tag = 2;
        
        
    }
    return self;
}
-(void)dealloc
{
    self.data=nil;
    self.indexPath=nil;
    self.key=nil;
    self.actionBlock=nil;
    [self.fuliView removeFromSuperview];
    self.fuliView=nil;
    [self.subjectV1View removeFromSuperview];
    self.subjectV1View=nil;
    [self.subjectV2View removeFromSuperview];
    self.subjectV2View=nil;
    
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}

/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    //todo: 需要测试 中间出现专题的情况
    //测试cell复用的情况
    self.key = key;
    self.indexPath=indexPath;
    NSDictionary *dict =(NSDictionary *)model;
    
    if(dict == nil|| [dict isKindOfClass:[NSDictionary class]] == NO)
        return;
    self.data =model;
    NSArray *subjectArray = [dict arrayObjectForKey:@"data"];
    if(subjectArray==nil ||![subjectArray isKindOfClass:[NSArray class]])
    {
        return;
    }
        float fMaxHeight =  [NiubiStyleCell_subject returnCellHeight:nil];
        _backView.frame = CGRectMake(PHOTO_TIME_PADDING, 0, DEVICE_WIDTH - PHOTO_FRAME_WIDTH, fMaxHeight);
        self.fuliView.frame = CGRectMake(PHOTO_FRAME_WIDTH, PHOTO_TIME_PADDING, DEVICE_WIDTH*3/5 - PHOTO_FRAME_WIDTH, fMaxHeight - PHOTO_TIME_PADDING*2);
     
        if([subjectArray count] > 0)
        {
            NSDictionary* dc = [subjectArray objectAtIndex:0];
            if(dc && [dc isKindOfClass:[NSDictionary class]])
            {
                NSString* strImageURL = [dc stringForKey:@"thumbnail" withDefaultValue:@""];//[dc objectForKey:@"thumbnail"];
                if(strImageURL && [strImageURL isKindOfClass:[NSString class]])
                {
                    [self.fuliView sd_setImageWithURL:  [NSURL URLWithString:strImageURL]];
                }
            }
        }
        self.subjectV1View.frame = CGRectMake(DEVICE_WIDTH*3/5, PHOTO_TIME_PADDING, DEVICE_WIDTH*2/5 - PHOTO_FRAME_WIDTH, fMaxHeight/2 - PHOTO_TIME_PADDING);
        
        
        if([subjectArray count] > 1)
        {
            NSDictionary* dc = [subjectArray objectAtIndex:1];
            if(dc && [dc isKindOfClass:[NSDictionary class]])
            {
                NSString* strImageURL =  [dc stringForKey:@"thumbnail" withDefaultValue:@""];
                if(strImageURL && [strImageURL isKindOfClass:[NSString class]])
                {
                    [self.subjectV1View sd_setImageWithURL:  [NSURL URLWithString:strImageURL]];
                }
            }
        }
        
        self.subjectV2View.frame = CGRectMake(DEVICE_WIDTH*3/5, self.subjectV1View.frame.origin.y+self.subjectV1View.frame.size.height, DEVICE_WIDTH*2/5 - PHOTO_FRAME_WIDTH, fMaxHeight/2 - PHOTO_TIME_PADDING );
        if([subjectArray count] > 2)
        {
            NSDictionary* dc = [subjectArray objectAtIndex:2];
            if(dc && [dc isKindOfClass:[NSDictionary class]])
            {
                NSString* strImageURL =  [dc stringForKey:@"thumbnail" withDefaultValue:@""];
                if(strImageURL && [strImageURL isKindOfClass:[NSString class]])
                {
                    [self.subjectV2View sd_setImageWithURL:  [NSURL URLWithString:strImageURL]];
                }
            }
        }

    
    
    
    [self layoutSubviews];
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    float xRate = DEVICE_WIDTH/375;
    return 150*xRate;
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
- (void) clickFuli:(UITapGestureRecognizer*)tap
{
    NSArray *subjectArray = [self.data arrayObjectForKey:@"data"];
    if (subjectArray && [subjectArray isKindOfClass:[NSArray class]]) {
        
        if(subjectArray == nil || [subjectArray count]==0)
            return;
        
        if(tap.view.tag >= [subjectArray count])
            return;
    
        
        if (self.actionBlock)
        {
            NSString *action=@"";
            if (tap.view.tag==0) {
                action =@"click_index_haitao";
                [UMengShare event:CK_INDEXHAITAO acc:1];
            }else if (tap.view.tag==1) {
                action =@"click_index_shaidan";
                [UMengShare event:CK_INDEXSHAIDAN acc:1];
            }else if (tap.view.tag==2) {
                action =@"click_index_zhuanti";
                [UMengShare event:CK_INDEXZHUANTI acc:1];
            }
            //统计页面
            NSMutableDictionary *dict =[NSMutableDictionary dictionary];
            [dict setValue:action forKey:@"action"];
            [dict setValue:@"" forKey:@"source"];
            [BaseModel statbuypage:dict success:^(AFHTTPRequestOperation *operation, id result) {
                
            } failure:^(NSError *error) {
                
            }];
            NSDictionary * dc = [subjectArray objectAtIndex:tap.view.tag];
            self.actionBlock(BEventType_Topic, tap.view, dc,nil,nil);
        }
        
    }
   
 
    
    
}
@end
